package com.pulaien.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.ProductUtil;
import com.pulaien.system.domain.dto.ExtGoodsParam;
import com.pulaien.system.domain.dto.ExtUploadFileDto;
import com.pulaien.system.domain.dto.UrlReq;
import com.pulaien.system.service.IConfigCategoryService;
import com.pulaien.system.service.IGoodsService;
import com.pulaien.system.service.ISearchRankingService;
import com.pulaien.system.service.Open1688Service;
import com.pulaien.system.service.alibaba.Billboard1688Service;
import com.pulaien.system.service.plug.GoodsPlService;
import com.pulaien.web.controller.common.OssController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.pulaien.common.utils.Validator.isUrl;


@RestController
@RequestMapping("/shop/goods")
@Api(tags = "商城端商品管理")
public class ShopGoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IConfigCategoryService configCategoryService;

    @Autowired
    private ISearchRankingService searchRankingService;

    @Autowired
    private Open1688Service open1688Service;

    @Autowired
    private Billboard1688Service billboard1688Service;

    @Autowired
    private OssController ossController;


    @Autowired
    private GoodsPlService goodsPlService;

    /**
     * 商品分类列表
     */
    @ApiOperation(value = "商品分类列表")
    @GetMapping("/categoryList")
    public AjaxResult categoryList() {
        JSONArray list = configCategoryService.getAllCategory();
        return AjaxResult.success(list);
    }

    /**
     * 根据父级ID获取子级商品分类
     */
    @ApiOperation(value = "根据父级ID获取子级商品分类")
    @GetMapping("/categoryListByParentId/{id}")
    public AjaxResult categoryListByParentId(@PathVariable Long id) {
        List<JSONObject> list = configCategoryService.categoryListByParentId(id);
        return AjaxResult.success(list);
    }


    /**
     * 精选商品列表
     */
    @ApiOperation(value = "精选商品列表")
    @GetMapping("/choiceList")
    public TableDataInfo recommendList(ExtGoodsParam extGoodsParam) {
        startPage();
        return goodsService.choiceList(extGoodsParam);
    }


    /**
     * 热门商品(销量前五)
     */
    @ApiOperation(value = "热门商品(销量前五)")
    @GetMapping("/hotList")
    public AjaxResult hotList() {
        List<JSONObject> list = goodsService.hotList();
        return AjaxResult.success(list);
    }


    /**
     * 根据图片URL搜索平台商品
     *
     * @param extGoodsParam 参数
     * @return
     */
    @ApiOperation(value = "根据图片地址搜索平台商品")
    @GetMapping("queryGoodsListByImg")
    public AjaxResult queryGoodsListByImg(ExtGoodsParam extGoodsParam) {

        String platform = extGoodsParam.getPlatform();
        try {

            if (StringUtils.isEmpty(platform) || StringUtils.isEmpty(extGoodsParam.getImgId()) || (!platform.equals("taobao") && !platform.equals("1688") && !platform.equals("jd"))) {
                return AjaxResult.success(new JSONObject());
            }

            return ProductUtil.searchImgInfo(extGoodsParam.getImgId(), extGoodsParam.getPageNum(), extGoodsParam.getPageSize(), extGoodsParam.getSortKey(), extGoodsParam.getSort(), extGoodsParam.getLowPrice(), extGoodsParam.getHighPrice(), platform);

        } catch (Exception e) {

            return AjaxResult.success(new JSONObject());
        }

    }






    @ApiOperation(value = "京东商品信息")
    @GetMapping("getGoodsInfoJd")
    public AjaxResult getGoodsInfoJd(@RequestParam("numIid") String numIid) {


        JSONObject jsonObject = ProductUtil.getGoodsInfoForJd(numIid);
        return AjaxResult.success(jsonObject);
    }



    /**
     * 拉取平台商品信息
     *
     * @param url
     * @return
     */
    @ApiOperation(value = "拉取平台商品信息")
    @GetMapping("getGoodsInfo")
    public AjaxResult getGoodsInfo(@RequestParam("url") String url) {

        if (url.isEmpty() ){
            return AjaxResult.success();
        }

        if(!url.contains("http")) {
            url = "https:" + url;
        }

        if(!isUrl(url)) {
            return AjaxResult.success();
        }

        JSONObject jsonObject = ProductUtil.getGoodsInfoForShop(url);
        return AjaxResult.success(jsonObject);
    }



    @ApiOperation(value = "拉取平台商品信息")
    @PostMapping("getGoodsInfoNew")
    public AjaxResult getGoodsInfoNew(@RequestBody UrlReq urlReq) {

        String url = urlReq.getUrl();
        if (url.isEmpty() || !url.contains("http") || !isUrl(url)){
            return AjaxResult.success();
        }

        JSONObject jsonObject = ProductUtil.getGoodsInfoForShop(url);
        return AjaxResult.success(jsonObject);
    }




    @ApiOperation(value = "京东关键字查询商品列表")
    @GetMapping("queryGoodsListByKeyWordJd")
    public AjaxResult queryGoodsListByKeyWordJd(
            ExtGoodsParam extGoodsParam,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {

        String platform = extGoodsParam.getPlatform();
        String keyWord = extGoodsParam.getKeyName();

        if (StringUtils.isEmpty(keyWord) || StringUtils.isEmpty(platform) || (!platform.equals("taobao") &&  !platform.equals("1688") && !platform.equals("jd"))) {
            return AjaxResult.success(new JSONObject());
        }


        if(extGoodsParam.getKeyName().length() > 200) {
            return AjaxResult.success(new JSONObject());
        }

        if (StringUtils.equals(lang, ConstantsUtil.LANG_ZH)){
            searchRankingService.updateTop(keyWord);
        }else if (StringUtils.equals(lang,ConstantsUtil.LANG_JA)){
            searchRankingService.updateTopJa(keyWord);
        } else {
            searchRankingService.updateTop(keyWord);
        }


        return ProductUtil.getInfoByKeyWordJd(keyWord, extGoodsParam.getPageNum(), extGoodsParam.getPageSize(), extGoodsParam.getSortKey(), extGoodsParam.getSort(), extGoodsParam.getLowPrice(), extGoodsParam.getHighPrice(), platform);

    }





    /**
     * 关键字查询商品列表
     *
     * @param extGoodsParam
     * @return
     */
    @ApiOperation(value = "关键字查询商品列表/淘宝")
    @GetMapping("queryGoodsListByKeyWord")
    public AjaxResult queryGoodsListByKeyWord(
            ExtGoodsParam extGoodsParam,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {

        String platform = extGoodsParam.getPlatform();
        String keyWord = extGoodsParam.getKeyName();

        if (StringUtils.isEmpty(keyWord) || StringUtils.isEmpty(platform) || (!platform.equals("taobao") &&  !platform.equals("1688") && !platform.equals("jd"))) {
            return AjaxResult.success(new JSONObject());
        }


        if(extGoodsParam.getKeyName().length() > 200) {
            return AjaxResult.success(new JSONObject());
        }

        if (StringUtils.equals(lang, ConstantsUtil.LANG_ZH)){
            searchRankingService.updateTop(keyWord);
        }else if (StringUtils.equals(lang,ConstantsUtil.LANG_JA)){
            searchRankingService.updateTopJa(keyWord);
        } else {
            searchRankingService.updateTop(keyWord);
        }


        return ProductUtil.getInfoByKeyWord(keyWord, extGoodsParam.getPageNum(), extGoodsParam.getPageSize(), extGoodsParam.getSortKey(), extGoodsParam.getSort(), extGoodsParam.getLowPrice(), extGoodsParam.getHighPrice(), platform);
    }



    @ApiOperation(value = "1688关键字查询商品列表")
    @GetMapping("/queryGoodsListBy1688")
    public AjaxResult queryGoodsListBy1688(ExtGoodsParam extGoodsParam , @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang) {




        return open1688Service.keywordQuery(extGoodsParam , lang);
    }


    @ApiOperation(value = "1688以图搜图上传图图片")
    @PostMapping("/uploadBy1688")
    public AjaxResult uploadBy1688(@Valid ExtUploadFileDto uploadFile) {


    return open1688Service.imageUpload(uploadFile);

    }



    @ApiOperation(value = "以图搜图上传图图片")
    @PostMapping("/uploadByAll")
    public AjaxResult uploadFIle(@Valid ExtUploadFileDto uploadFile) {


        AjaxResult ajaxResult = ossController.uploadFile(uploadFile);
        String jd = (String)ajaxResult.get("url");
        String tb = goodsPlService.uploadByTbPathNew(jd);
        return open1688Service.imageUploadAll(uploadFile , jd , tb);

    }














    @ApiOperation(value = "1688以图搜图查询商品列表")
    @GetMapping("/queryGoodsListBy1688Img")
    public AjaxResult queryGoodsListBy1688Img(
            @RequestParam(value = "imageId" ) String imageId,
            ExtGoodsParam extGoodsParam,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {

        return open1688Service.imgQuery(imageId , extGoodsParam , lang);

    }




    @ApiOperation(value = "1688商品详情")
    @GetMapping("/searchQueryProductDetail")
    public AjaxResult searchQueryProductDetail(
            @RequestParam(value = "offerId" ) Long offerId,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {

        return open1688Service.queryProductDetail(offerId , lang);
    }



    @GetMapping("/searchDetailHot")
    public AjaxResult hot(
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang,
            @RequestParam(value = "categoryId") String categoryId
    ) {
        System.out.println("categoryId  "  + categoryId);
        return billboard1688Service.hot(lang, categoryId , 8);
    }






    /**
     * 查询商品信息列表
     */
    @ApiOperation(value = "商品列表-本地商品")
    @GetMapping("/list")
    public TableDataInfo list(ExtGoodsParam extGoodsParam) {
        if ("asc".equals(extGoodsParam.getSort())) {
            extGoodsParam.setSort("ASC");
        } else {
            extGoodsParam.setSort("DESC");
        }

        startPage();
        return goodsService.listForCustomer(extGoodsParam);
    }


    /**
     * 查询商品详情
     */
    @ApiOperation(value = "商品详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        JSONObject goods = goodsService.info(id);
        return AjaxResult.success(goods);
    }


    public static void main(String[] args) {

        JSONObject jd = ProductUtil.getDetailInfoForShop("10083810655446", "jd");

        System.out.println("-----------------------------------");
        System.out.println(jd);
    }


}
